package tcc.board.protocol;

import java.util.Scanner;
import java.util.regex.MatchResult;

import tcc.board.peripherials.Switches;
import tcc.board.peripherials.Switches.SwitchState;

public class GetSwitchesProtocol {
	private static final String PROTOCOL_GET_SWITCHES_INPUT = "GET_SWITCHES";
	private static final String PROTOCOL_GET_SWITCHES_OUTPUT = "^GET_SWITCHES_([01])([01])([01])$";
	
	public static String buildRequest() {
		return PROTOCOL_GET_SWITCHES_INPUT;
	}
	
	public static Switches parseResponse(String response) {
		Scanner scan = new Scanner(response);
		scan.findInLine(PROTOCOL_GET_SWITCHES_OUTPUT);
        MatchResult result = scan.match();
        
        return new Switches(
        		result.group(1).contentEquals("1") ? SwitchState.SWITCH_ON : SwitchState.SWITCH_OFF,
        		result.group(2).contentEquals("1") ? SwitchState.SWITCH_ON : SwitchState.SWITCH_OFF,
        		result.group(3).contentEquals("1") ? SwitchState.SWITCH_ON : SwitchState.SWITCH_OFF);
	}
}
